package com.lyl.momanapp.frament;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lyl.momanapp.R;
import com.lyl.momanapp.activity.FaceActivity;
import com.lyl.momanapp.adapter.TwoTwoAdapter;
import com.lyl.momanapp.adapter.TwoTwoLXAdapter;
import com.lyl.momanapp.adapter.TwoTwoimgAdapter;
import com.lyl.momanapp.bean.CJBean;
import com.lyl.momanapp.bean.Facials;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.face.net.DetectWork;
import com.lyl.momanapp.face.utils.ConvertUtils;
import com.lyl.momanapp.face.utils.Utils;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.ClipImageView;
import com.lyl.momanapp.util.view.MyFragment;
import com.lyl.momanapp.view.HorizontalListView;
import com.lyl.momanapp.view.HorizontalListViewAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinlan.imageeditlibrary.editimage.fliter.PhotoProcessing;
import com.xps.face.bean.BitmapBean;
import com.xps.face.bean.FaceDetectResp;
import com.xps.face.entities.PointSimple;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import rx.Subscriber;

/**
 * 名字 梁亚龙
 * 时间 2017/11/4.
 * QQ邮箱 503934353@qq.com
 */

public class TwoTwo extends MyFragment {
    FaceDetectResp.FacesBean.LandmarkBean landmarkBean;
    int width;
    int height;
    BitmapBean bitmapBean = new BitmapBean();
    Bitmap topBitmap = null;
    ImageView faceImage;
    private TwoTwoAdapter twoTwoAdapter;
    private TwoTwoimgAdapter twoTwoimgAdapter;
    private TwoTwoLXAdapter twoTwoLXAdapter;
    RecyclerView mRl;
    RecyclerView mRlimg;
    ImageView faxing, lianxing;
    private Dialog dialog;
    private BeautyTask handleTask;
    Bitmap getTopBitmap = null;
    private View inflate;
    private ImageView mFaceZhaoXiang;
    private RelativeLayout mRlimgseji;
    private WeakReference<Bitmap> resultBitmapRef;
    private ClipImageView.Style mStyle = ClipImageView.Style.RECTANGLE;
    private static final float BITMAP_SCALE = 0.4f;
    String lianxingstr = "1";
    String faxingstr = "1";
    ImageView beijing_Image;

    HorizontalListView hListView;

    HorizontalListViewAdapter hListViewAdapter;

    private final class BeautyTask extends AsyncTask<Integer, Void, Bitmap> {
        private float smoothVal;
        private float whiteVal;

        private Bitmap srcBitmap;

        public BeautyTask(float smooth, double white) {
            this.smoothVal = smooth;
            this.whiteVal = (float) white;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            srcBitmap = Bitmap.createBitmap(getTopBitmap.copy(
                    Bitmap.Config.ARGB_8888, true));
            PhotoProcessing.handleSmoothAndWhiteSkin(srcBitmap, smoothVal, whiteVal);
            return srcBitmap;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        protected void onCancelled(Bitmap result) {
            super.onCancelled(result);
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            if (result == null)
                return;
            resultBitmapRef = new WeakReference<Bitmap>(result);
            try {
                saveFile(resultBitmapRef.get(), "asdasd4.jpg");
                JsonBean(Environment.getExternalStorageDirectory() + File.separator + "asdasd4.jpg");
                Log.e("qweqwe", "2");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void doBeautyTask() {
        if (handleTask != null && !handleTask.isCancelled()) {
            handleTask.cancel(true);
        }

        handleTask = new BeautyTask(0, 10);
        Log.e("qweqwe", "1");
        handleTask.execute(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.face_Image: {
                loaddialogdata();
            }
            break;
            case R.id.face_zhaoxiao: {
                dialog.cancel();
                startActivity(new Intent(getActivity(), FaceActivity.class));
            }
            break;
        }
    }

    @Override
    public int inflaterRootView() {
        return R.layout.frament_two_two;
    }

    @Override
    public void initUI() {
        faxing = findView(R.id.faxing);
        lianxing = findView(R.id.lianxing);
        faceImage = findView(R.id.face_Image);
        mRlimg = findView(R.id.lian_dong_rl_img);
        mRl = findView(R.id.lian_dong_rl_name);
        mRlimgseji = findView(R.id.two_two_rl);
        beijing_Image = findView(R.id.beijing_Image);
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(getActivity().WINDOW_SERVICE);
        final int width = wm.getDefaultDisplay().getWidth();
        final int height = wm.getDefaultDisplay().getHeight();
        final int[] x = {0};
        final int[] y = {0};
        final LayoutParams[] para = new LayoutParams[1];
        Log.i("CJBean.beijing", CJBean.beijing + "");
        if (CJBean.beijing == 1) {
            para[0] = faxing.getLayoutParams();
            para[0].width = (int) (100.465 / 375 * width);
            para[0].height = (int) (80.522 / 667 * height);
            x[0] = (int) (133.360 / 375 * width);
            y[0] = (int) (50.010 / 667 * height);

            beijing_Image.setImageDrawable(getResources().getDrawable(R.drawable.mansoldier));
            beijing_Image.setDrawingCacheEnabled(false);
        } else {
            para[0] = faxing.getLayoutParams();
            para[0].width = (int) (100.465 / 375 * width);
            para[0].height = (int) (80.522 / 667 * height);
            x[0] = (int) (133.360 / 375 * width);
            y[0] = (int) (50.010 / 667 * height);

            beijing_Image.setImageDrawable(getResources().getDrawable(R.drawable.mansoldier1));
            beijing_Image.setDrawingCacheEnabled(false);
        }
        setMargins(faxing, x[0], y[0], 0, 0);
        faxing.setLayoutParams(para[0]);
        //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_1));
        //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
        faxing.setDrawingCacheEnabled(false);


    }

    ArrayList<Point> mPointList = new ArrayList<>();

    public void seji() {
        String image = new MyUtils().saveImage(mRlimgseji, new MyUtils().getTime(), getContext());
        URL.sejiURL = image;

    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {
        faceImage.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRl.setLayoutManager(linearLayoutManager);
        mRl.setHasFixedSize(true);
        twoTwoAdapter = new TwoTwoAdapter(new String[]{"头发", "脸型"});
        mRl.setAdapter(twoTwoAdapter);
        twoTwoAdapter.setOnItemClickLitener(new TwoTwoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(TextView view, int position) {
                if (position == 0) {
                    mRlimg.setAdapter(twoTwoimgAdapter);
                    twoTwoimgAdapter.showDataString(new String[]{"1", "2"});
                } else {
                    mRlimg.setAdapter(twoTwoLXAdapter);
                    twoTwoLXAdapter.showDataString(new String[]{"1", "2"});
                }
            }
        });
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRlimg.setLayoutManager(linearLayoutManager2);
        mRlimg.setHasFixedSize(true);
        twoTwoimgAdapter = new TwoTwoimgAdapter(new String[]{}, getActivity());
        twoTwoimgAdapter.showDataString(new String[]{"1", "2"});
        mRlimg.setAdapter(twoTwoimgAdapter);
        twoTwoLXAdapter = new TwoTwoLXAdapter(new String[]{}, getActivity());

        WindowManager wm = (WindowManager) getContext()
                .getSystemService(getActivity().WINDOW_SERVICE);
        final int width = wm.getDefaultDisplay().getWidth();
        final int height = wm.getDefaultDisplay().getHeight();
        final int[] x = {0};
        final int[] y = {0};
        final LayoutParams[] para = new LayoutParams[1];

        if (CJBean.beijing == 1) {
            para[0] = faxing.getLayoutParams();
            para[0].width = (int) (53.465 / 375 * width);
            para[0].height = (int) (94.522 / 667 * height);
            x[0] = (int) (160.360 / 375 * width);
            y[0] = (int) (108.010 / 667 * height);
        } else {
            para[0] = faxing.getLayoutParams();
            para[0].width = (int) (53.465 / 375 * width);
            para[0].height = (int) (94.522 / 667 * height);
            x[0] = (int) (160.360 / 375 * width);
            y[0] = (int) (28.010 / 667 * height);
        }


        setMargins(faxing, x[0], y[0], 0, 0);
        faxing.setLayoutParams(para[0]);
        //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_1));
        //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
        faxing.setDrawingCacheEnabled(false);


        if (CJBean.beijing == 1) {
            para[0] = lianxing.getLayoutParams();
            para[0].width = (int) (150.465 / 375 * width);
            para[0].height = (int) (70.522 / 667 * height);
            x[0] = (int) (112.360 / 375 * width);
            y[0] = (int) (136.010 / 667 * height);
        } else {
            para[0] = lianxing.getLayoutParams();
            para[0].width = (int) (150.465 / 375 * width);
            para[0].height = (int) (70.522 / 667 * height);
            x[0] = (int) (112.360 / 375 * width);
            y[0] = (int) (54.010 / 667 * height);
        }

        setMargins(lianxing, x[0], y[0], 0, 0);
        lianxing.setLayoutParams(para[0]);
        //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
        lianxing.setImageDrawable(getResources().getDrawable(R.drawable.lianxing1));
        //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
        lianxing.setDrawingCacheEnabled(false);


        twoTwoimgAdapter.setOnItemClickLitener(new TwoTwoimgAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(ImageView view, int position) {
                if (position == 0) {
                    faxingstr = "1";
                    if (CJBean.beijing == 1) {
                        if (lianxingstr.equals("2")) {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (108.010 / 667 * height);

                        } else {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (108.010 / 667 * height);
                        }
                    } else {
                        if (lianxingstr.equals("2")) {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (28.010 / 667 * height);
                        } else {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (28.010 / 667 * height);
                        }
                    }


                    setMargins(faxing, x[0], y[0], 0, 0);
                    faxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_1));
                    //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
                    faxing.setDrawingCacheEnabled(false);

                } else {
                    faxingstr = "2";

                    if (CJBean.beijing == 1) {
                        if (lianxingstr.equals("2")) {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (108.010 / 667 * height);

                        } else {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (108.010 / 667 * height);

                        }
                    } else {
                        if (lianxingstr.equals("2")) {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (28.010 / 667 * height);

                        } else {
                            para[0] = faxing.getLayoutParams();
                            para[0].width = (int) (53.465 / 375 * width);
                            para[0].height = (int) (94.522 / 667 * height);
                            x[0] = (int) (160.360 / 375 * width);
                            y[0] = (int) (28.010 / 667 * height);

                        }
                    }


                    setMargins(faxing, x[0], y[0], 0, 0);
                    faxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_2));
                    //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
                    faxing.setDrawingCacheEnabled(false);
                }
            }
        });
        twoTwoLXAdapter.setOnItemClickLitener(new TwoTwoLXAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(ImageView view, int position) {
                if (position == 0) {
                    lianxingstr = "1";
                    if (CJBean.beijing == 1) {
                        para[0] = faceImage.getLayoutParams();
                        para[0].width = (int) (41.465 / 375 * width);
                        para[0].height = (int) (43.522 / 667 * height);
                        x[0] = (int) (167.360 / 375 * width);
                        y[0] = (int) (160.010 / 667 * height);
                    } else {
                        para[0] = faceImage.getLayoutParams();
                        para[0].width = (int) (41.465 / 375 * width);
                        para[0].height = (int) (43.522 / 667 * height);
                        x[0] = (int) (167.360 / 375 * width);
                        y[0] = (int) (80.010 / 667 * height);
                    }
                    setMargins(faceImage, x[0], y[0], 0, 0);
                    faceImage.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    faceImage.setImageBitmap(replaceBitmapColor(topBitmap));
                    faceImage.setDrawingCacheEnabled(false);


                    if (CJBean.beijing == 1) {
                        para[0] = faxing.getLayoutParams();
                        para[0].width = (int) (53.465 / 375 * width);
                        para[0].height = (int) (94.522 / 667 * height);
                        x[0] = (int) (160.360 / 375 * width);
                        y[0] = (int) (108.010 / 667 * height);
                    } else {
                        para[0] = faxing.getLayoutParams();
                        para[0].width = (int) (53.465 / 375 * width);
                        para[0].height = (int) (94.522 / 667 * height);
                        x[0] = (int) (160.360 / 375 * width);
                        y[0] = (int) (28.010 / 667 * height);
                    }

                    setMargins(faxing, x[0], y[0], 0, 0);
                    faxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    if (faxingstr.equals("1")) {
                        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_1));
                    } else {
                        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_2));
                    }
                    faxing.setDrawingCacheEnabled(false);


                    if (CJBean.beijing == 1) {
                        para[0] = lianxing.getLayoutParams();
                        para[0].width = (int) (150.465 / 375 * width);
                        para[0].height = (int) (70.522 / 667 * height);
                        x[0] = (int) (112.360 / 375 * width);
                        y[0] = (int) (136.010 / 667 * height);
                    } else {
                        para[0] = lianxing.getLayoutParams();
                        para[0].width = (int) (150.465 / 375 * width);
                        para[0].height = (int) (70.522 / 667 * height);
                        x[0] = (int) (112.360 / 375 * width);
                        y[0] = (int) (54.010 / 667 * height);
                    }

                    setMargins(lianxing, x[0], y[0], 0, 0);
                    lianxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    lianxing.setImageDrawable(getResources().getDrawable(R.drawable.lianxing1));
                    //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
                    lianxing.setDrawingCacheEnabled(false);
                } else {
                    lianxingstr = "2";
                    if (CJBean.beijing == 1) {
                        para[0] = faceImage.getLayoutParams();
                        para[0].width = (int) (41.465 / 375 * width);
                        para[0].height = (int) (43.522 / 667 * height);
                        x[0] = (int) (167.360 / 375 * width);
                        y[0] = (int) (160.010 / 667 * height);
                    } else {
                        para[0] = faceImage.getLayoutParams();
                        para[0].width = (int) (41.465 / 375 * width);
                        para[0].height = (int) (43.522 / 667 * height);
                        x[0] = (int) (167.360 / 375 * width);
                        y[0] = (int) (80.010 / 667 * height);
                    }


                    setMargins(faceImage, x[0], y[0], 0, 0);
                    faceImage.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    faceImage.setImageBitmap(replaceBitmapColor(topBitmap));
                    faceImage.setDrawingCacheEnabled(false);

                    if (CJBean.beijing == 1) {
                        para[0] = faxing.getLayoutParams();
                        para[0].width = (int) (53.465 / 375 * width);
                        para[0].height = (int) (94.522 / 667 * height);
                        x[0] = (int) (160.360 / 375 * width);
                        y[0] = (int) (108.010 / 667 * height);
                    } else {
                        para[0] = faxing.getLayoutParams();
                        para[0].width = (int) (53.465 / 375 * width);
                        para[0].height = (int) (94.522 / 667 * height);
                        x[0] = (int) (160.360 / 375 * width);
                        y[0] = (int) (28.010 / 667 * height);
                    }


                    setMargins(faxing, x[0], y[0], 0, 0);
                    faxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    if (faxingstr.equals("1")) {
                        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_1));
                    } else {
                        faxing.setImageDrawable(getResources().getDrawable(R.drawable.faxing_image_2));
                    }
                    faxing.setDrawingCacheEnabled(false);

                    if (CJBean.beijing == 1) {
                        para[0] = lianxing.getLayoutParams();
                        para[0].width = (int) (150.465 / 375 * width);
                        para[0].height = (int) (70.522 / 667 * height);
                        x[0] = (int) (112.360 / 375 * width);
                        y[0] = (int) (136.010 / 667 * height);
                    } else {
                        para[0] = lianxing.getLayoutParams();
                        para[0].width = (int) (150.465 / 375 * width);
                        para[0].height = (int) (70.522 / 667 * height);
                        x[0] = (int) (112.360 / 375 * width);
                        y[0] = (int) (54.010 / 667 * height);
                    }
                    setMargins(lianxing, x[0], y[0], 0, 0);
                    lianxing.setLayoutParams(para[0]);
                    //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
                    lianxing.setImageDrawable(getResources().getDrawable(R.drawable.lianxing2));
                    //faxing.setImageBitmap(replaceBitmapColor(topBitmap));
                    lianxing.setDrawingCacheEnabled(false);
                }
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            Log.i("111111111", "onHiddenChanged: " + 11111);
        } else {
            Log.i("111111111", "onHiddenChanged: " + 222222222);
            if (URL.imgURL == null) {
                Log.e("onHiddenChangedURL", "onHiddenChangedfalse");
            } else {
                // getTopBitmap = getAlplaBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "asdasd.jpg"), 70);
                getTopBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "asdasd.jpg");
                doBeautyTask();
                Log.e("onHiddenChangedURL", URL.imgURL);
            }
        }
    }


    public void show1() {

        dialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item_face, null);

        hListView = inflate.findViewById(R.id.hListView);

        HorizontalListViewAdapter hListViewAdapter;

        mFaceZhaoXiang = (ImageView) inflate.findViewById(R.id.face_zhaoxiao);
        mFaceZhaoXiang.setOnClickListener(this);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        lp.y = 20;//设置Dialog距离底部的距离
        //       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
    }


    void loadview(final List<Facials> allFacials) {

        hListViewAdapter = new HorizontalListViewAdapter(getActivity(), allFacials);

        dialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item_face, null);

        hListView = inflate.findViewById(R.id.hListView);
        hListView.setAdapter(hListViewAdapter);


        hListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ImageLoader.getInstance().displayImage(allFacials.get(position).getThumbnail(), faceImage);
                dialog.dismiss();
            }
        });

        mFaceZhaoXiang = inflate.findViewById(R.id.face_zhaoxiao);
        mFaceZhaoXiang.setOnClickListener(this);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
        lp.y = 20;//设置Dialog距离底部的距离
        //       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框

    }


    void loaddialogdata() {
        HashMap<String, String> map = new HashMap<>();
//        map.put("phone", mPhone.getText().toString().trim());
//        map.put("access_type", 1 + "");
//        map.put("password", mPwd.getText().toString().trim());
        new MyUtils().xUtilsPost(URL.URL_FacialsList, map, handler, URL.ID_FacialsList);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case URL.ID_FacialsList: {
                    try {
                        JSONObject json = new JSONObject(msg.obj.toString());
                        String state = json.getString("state");
                        if (state.equalsIgnoreCase("M00000")) {
                            JSONObject result = json.getJSONObject("result");
                            JSONArray data = result.getJSONArray("data");

                            List<Facials> allFacials = new ArrayList<>();
                            for (int i = 0; i < data.length(); i++) {
                                int facial_id = data.getJSONObject(i).getInt("facial_id");
                                String original_image = data.getJSONObject(i).getString("original_image");
                                String thumbnail = data.getJSONObject(i).getString("thumbnail");
                                String preview_image = data.getJSONObject(i).getString("preview_image");

                                Facials fac = new Facials(facial_id, original_image, thumbnail, preview_image);
                                allFacials.add(fac);
                            }
                            loadview(allFacials);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.i("111", "handleMessage: " + msg.obj.toString());
                }
                break;
            }
        }
    };


    public void JsonBean(String str) {
        final String file = str;
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;
        DetectWork.doDetect(Utils.getImageStr(file), new Subscriber<FaceDetectResp>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(FaceDetectResp faceDetectResp) {
                if (faceDetectResp.getFaces() == null || faceDetectResp.getFaces().size() < 1)
                    return;
                landmarkBean = faceDetectResp.getFaces().get(0).getLandmark();
                Path path = getMouthPath(ConvertUtils.getPoints(landmarkBean, width, height));
                try {
                    setBitmap(file, path);
                    Log.e("qweqwe", "3");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    Path getMouthPath(LinkedList<PointSimple> points) {
        Path path = new Path();
        PointSimple p0 = points.get(0);
        path.moveTo(p0.width_scale, p0.height_scale);
        for (PointSimple ps : points) {
            path.lineTo(ps.width_scale, ps.height_scale);
            Log.e("getMouthPath", ps.width_scale + "----" + ps.height_scale);
        }
        path.lineTo(p0.width_scale, p0.height_scale);
        path.close();
        return path;
    }

    public void setBitmap(String top, Path clipPath) throws IOException {
        this.topBitmap = BitmapFactory.decodeFile(top);//fun(,clipPath);
        topBitmap = bitmapBean.fun(topBitmap, clipPath);
        //faceImage.setImageBitmap(topBitmap);
        //faceImage.setPadding(320,200,0,0);
        Log.e("qweqwe", "4");
        setBitmap();
        //        Drawable drawable = new BitmapDrawable(replaceBitmapColor(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + F  ile.separator + "asdasd1.jpg")));
        //        faceImage.setBackgroundDrawable(drawable);
    }

    public void setBitmap() throws IOException {
        if (mStyle == ClipImageView.Style.RECTANGLE) {
            float left = CJBean.startX > CJBean.endX ? CJBean.endX : CJBean.startX;
            float top = CJBean.startY > CJBean.endY ? CJBean.endY : CJBean.startY;
            Log.e("setBitmap", CJBean.startX + "");
            if (left <= 0 || top <= 0) {
                return;
            }
            Bitmap bitmap = getBitmap();

            Log.i("setBitmap", CJBean.endX + ":::::::" + CJBean.startX + ":::::::" + CJBean.endY + "::::::::" + CJBean.startY);
            topBitmap = Bitmap.createBitmap(bitmap, (int) left, (int) top, (int) Math.abs(CJBean.endX - CJBean.startX), (int)
                    Math.abs(CJBean.endY - CJBean.startY));

            WindowManager wm = (WindowManager) getContext()
                    .getSystemService(getActivity().WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            int height = wm.getDefaultDisplay().getHeight();
            int x = 0;
            int y = 0;
            LayoutParams para;
            para = faceImage.getLayoutParams();

            if (CJBean.beijing == 1) {
                para.width = (int) (41.465 / 375 * width);
                para.height = (int) (43.522 / 667 * height);
                x = (int) (167.360 / 375 * width);
                y = (int) (160.010 / 667 * height);
            } else {
                para.width = (int) (41.465 / 375 * width);
                para.height = (int) (43.522 / 667 * height);
                x = (int) (167.360 / 375 * width);
                y = (int) (80.010 / 667 * height);
            }

            setMargins(faceImage, x, y, 0, 0);
            faceImage.setLayoutParams(para);
            //faceImage.setImageBitmap(BlurBitmapUtil.blurBitmap(getActivity(), topBitmap, 1));
            faceImage.setImageBitmap(replaceBitmapColor(topBitmap));
            faceImage.setDrawingCacheEnabled(false);
            // faceImage.getBackground().setAlpha(50);
            reset();
        }
    }

    public static void setMargins(View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    private void reset() {
        CJBean.startX = 0;
        CJBean.startY = 0;
        CJBean.endX = 0;
        CJBean.endY = 0;
        mPointList.clear();
    }

    public Bitmap getBitmap() {

        return topBitmap;
    }

    private Path getPath(ArrayList<Point> pointList) {
        Path path = new Path();
        boolean first = true;
        for (Point p : pointList) {
            if (first) {
                path.moveTo(p.x, p.y);
                first = false;
            } else {
                path.lineTo(p.x, p.y);
            }
        }
        path.close();
        return path;
    }

    public void saveFile(Bitmap bm, String fileName) throws IOException {
        String path = Environment.getExternalStorageDirectory() + File.separator;
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        File myCaptureFile = new File(path + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
    }

    public static Bitmap replaceBitmapColor(Bitmap oldBitmap) {
        Bitmap mBitmap = oldBitmap.copy(Bitmap.Config.ARGB_8888, true);
        int mBitmapWidth = mBitmap.getWidth();
        int mBitmapHeight = mBitmap.getHeight();

        for (int i = 0; i < mBitmapHeight; ++i) {
            for (int j = 0; j < mBitmapWidth; ++j) {
                int color = mBitmap.getPixel(j, i);
                int red = (color & 16711680) >> 16;
                int green = (color & '\uff00') >> 8;
                int blue = color & 255;
                if (blue > 120 && green > 120 && red > 120 && blue <= 255 && green <= 255 && red <= 255) {
                    mBitmap.setPixel(j, i, Color.argb(100, red, green, blue));
                }
            }
        }
        return mBitmap;
    }

    public static Bitmap getAlplaBitmap(Bitmap sourceImg, int number) {
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());
        number = number * 255 / 100;
        for (int i = 0; i < argb.length; i++) {
            argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
        }
        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Bitmap.Config.ARGB_8888);
        return sourceImg;
    }
}
